package com.greate.community.controller;

import com.greate.community.entity.DiscussPost;
import com.greate.community.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/search")
public class ElasticsearchController {

    @Autowired
    private ElasticsearchService elasticsearchService;

    /**
     * 保存帖子到Elasticsearch
     * @param post 帖子实体
     */
    @PostMapping("/save")
    public String saveDiscussPost(@RequestBody DiscussPost post) {
        elasticsearchService.saveDiscusspost(post);
        return "Post saved successfully!";
    }

    /**
     * 从Elasticsearch删除帖子
     * @param id 帖子ID
     */
    @DeleteMapping("/delete/{id}")
    public String deleteDiscussPost(@PathVariable int id) {
        elasticsearchService.deleteDiscusspost(id);
        return "Post deleted successfully!";
    }

    /**
     * 根据关键词进行分页搜索
     * @param keyword 搜索关键词
     * @param page 当前页码
     * @param size 每页显示的记录数
     * @return 分页结果
     */
    @GetMapping("/search")
    public Page<DiscussPost> searchDiscussPost(@RequestParam String keyword,
                                               @RequestParam(defaultValue = "0") int page,
                                               @RequestParam(defaultValue = "10") int size) {
        return elasticsearchService.searchDiscussPost(keyword, page, size);
    }
}